package eco.lamp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TextView.BufferType;

import com.android.custom.NumberPicker;
import com.android.custom.NumberPicker.OnValueChangeListener;

import eco.lamp.bl.LampItem;
import eco.lamp.bl.LampUtil;

public class LampActivity extends Activity {

	int mUpgradeStep = 0;
	LampItem mLamp;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.new_lamp);
		mUpgradeStep = getIntent().getIntExtra("UpgradeStep", 0);
		int lampNumber = getIntent().getIntExtra("LampNumber", 1);
		final int lampType = getIntent().getIntExtra("LampType",
				LampUtil.LAMP_TYPE_INCADESCENT);
		mLamp = new LampItem(lampType, 0, 0);

		// show hazard attention info for fluorescent lamp type
		if (lampType == LampUtil.LAMP_TYPE_FLUORESCENT) {
			TextView vwAttention = (TextView) findViewById(R.id.vwAttention);
			vwAttention.setText(Html.fromHtml(getResources().getString(
					R.string.strAttention)), BufferType.SPANNABLE);
			vwAttention.setMovementMethod(LinkMovementMethod.getInstance());
			vwAttention.setVisibility(View.VISIBLE);
		}

		String lampInfo = "";
		int photoID = 0;
		switch (lampType) {
		case LampUtil.LAMP_TYPE_INCADESCENT:
			lampInfo = getResources().getString(R.string.strLampInfoInc);
			photoID = R.drawable.lamp_photo_inc;
			break;
		case LampUtil.LAMP_TYPE_FLUORESCENT:
			lampInfo = getResources().getString(R.string.strLampInfoFlr);
			photoID = R.drawable.lamp_photo_flr;
			break;
		case LampUtil.LAMP_TYPE_LED:
			lampInfo = getResources().getString(R.string.strLampInfoLed);
			photoID = R.drawable.lamp_photo_led;
			break;
		default:
			break;
		}

		ImageView imgLampPhoto = (ImageView) findViewById(R.id.imgLampPhoto);
		imgLampPhoto.setImageResource(photoID);

		TextView txtLampInfo = (TextView) findViewById(R.id.txtLampInfo);
		txtLampInfo.setText(Html.fromHtml(lampInfo), BufferType.SPANNABLE);
		txtLampInfo.setMovementMethod(LinkMovementMethod.getInstance());

		final NumberPicker editLampPower = (NumberPicker) findViewById(R.id.npLampPower);
		editLampPower.setRange(5, LampUtil.getMaxPower(lampType));
		editLampPower.setChangeAmount(5);
		editLampPower.setCurrent(LampUtil.getDefaultPower(lampType));

		final NumberPicker editLampNumber = (NumberPicker) findViewById(R.id.npLampNumber);
		editLampNumber.setRange(1, 100);
		editLampNumber.setCurrent(lampNumber);

		final TextView txtOutValue = (TextView) findViewById(R.id.txtLampOutput);

		updateOutput(lampType, editLampPower.getCurrent(), editLampNumber
				.getCurrent(), txtOutValue);

		editLampNumber.setOnValueChangeListener(new OnValueChangeListener() {
			public void valueChanged(int value) {
				updateOutput(lampType, editLampPower.getCurrent(), value,
						txtOutValue);
			}
		});

		editLampPower.setOnValueChangeListener(new OnValueChangeListener() {
			public void valueChanged(int value) {
				updateOutput(lampType, value, editLampNumber.getCurrent(),
						txtOutValue);
			}
		});

		Button btnNext = (Button) findViewById(R.id.btnLampNext);

		btnNext.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				saveLamp();
				Bundle results = new Bundle();
				results.putParcelable("Lamp", mLamp);
				Intent i = getIntent();
				i.putExtras(results);
				setResult(RESULT_OK, i);
				finish();
			}
		});

		Button btnBack = (Button) findViewById(R.id.btnLampBack);
		btnBack.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				setResult(RESULT_CANCELED);
				finish();
			}
		});
	}

	private void updateOutput(int lampType, int power, int number,
			final TextView txtOutValue) {
		int result = LampUtil.getOutput(lampType, power) * number;
		txtOutValue.setText(String.valueOf(result));
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		saveLamp();
		outState.putParcelable("Lamp", mLamp);
		outState.putInt("UpgradeStep", mUpgradeStep);

	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		mUpgradeStep = savedInstanceState.getInt("UpgradeStep");
		mLamp = savedInstanceState.getParcelable("Lamp");
		loadLamp();
	}

	private void saveLamp() {
		NumberPicker editLampPower = (NumberPicker) findViewById(R.id.npLampPower);
		NumberPicker editLampNumber = (NumberPicker) findViewById(R.id.npLampNumber);
		editLampPower.updateCurrentValue();
		editLampNumber.updateCurrentValue();
		mLamp.setPower(editLampPower.getCurrent());
		mLamp.setNumber(editLampNumber.getCurrent());
	}

	private void loadLamp() {
		NumberPicker editLampPower = (NumberPicker) findViewById(R.id.npLampPower);
		NumberPicker editLampNumber = (NumberPicker) findViewById(R.id.npLampNumber);
		editLampPower.setCurrent(mLamp.getPower());
		editLampNumber.setCurrent(mLamp.getNumber());
	}
}
